<?php

namespace app\model;

use think\Model;

class Operatingcity extends Model
{
    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'operatingcity';

    public static function getTitle($id = '')
    {
        $mo = self::find($id);
        return $mo->title;
    }
    public static function getCityid($id = '')
    {
        $mo = self::find($id);
        return $mo->city_id;
    }

    public static function getCityname($id = '')
    {
        $mo = self::find($id);
        return $mo->city_name;
    }


    public static function setIncome($order_info)
    {
        if (!empty($order_info['shipping_city_name'])) {
            $Operatingcity1 = self::where([
                'areatype' => 1,
                'city_name' => $order_info['shipping_city_name']
            ])->order('id asc')->find();

            if (!empty($Operatingcity1)) {
                self::calculate($order_info, $Operatingcity1);
            }
        }

        if (!empty($order_info['shipping_district_name'])) {
            $Operatingcity2 = self::where([
                'areatype' => 2,
                'district_name' => $order_info['shipping_district_name']
            ])->order('id asc')->find();

            if (!empty($Operatingcity2)) {
                self::calculate($order_info, $Operatingcity2);
            }
        }
    }

    public static function calculate($order_info, $Operatingcity)
    {

        if (!empty($Operatingcity)) {
            $Operatingcity = $Operatingcity->toArray();
        }

        if (!empty($Operatingcity['status'])) {
            $percent = OperatingcityLevel::getPercent($Operatingcity['level']);

            if (empty($percent)) {
                $percent = Config::getconfig('operatingcity')['return_percent'];
            }

            if ((int)$percent > 0) {

                $income = ($order_info['total'] * percent_to_num($percent));

                $return_percent = $percent;

                if ($income > 0 && $income < 0.01) {
                    $income = 0.01;
                }

                Operatingcity::where('id', $Operatingcity['id'])->inc('income', $income)->update();
                Operatingcity::where('id', $Operatingcity['id'])->inc('total_income', $income)->update();

                $incomedata['ocid'] = $Operatingcity['id'];
                $incomedata['ptype'] = 1;
                $incomedata['weid'] = weid();
                $incomedata['order_id'] = $order_info['id'];
                $incomedata['order_num_alias'] = $order_info['order_num_alias'];
                $incomedata['buyer_id'] = $order_info['uid'];
                $incomedata['income'] = $income;
                $incomedata['return_percent'] = $return_percent;
                $incomedata['percentremark'] = $order_info['total'] . '% x' . $percent . '%';
                $incomedata['order_total'] = $order_info['total'];
                $incomedata['pay_time'] = $order_info['pay_time'];
                $incomedata['create_time'] = time();
                $incomedata['month_time'] = date('m', time());
                $incomedata['year_time'] = date('Y', time());
                $incomedata['order_status_id'] = Config::getconfig()['paid_order_status_id'];

                OperatingcityIncomelog::create($incomedata);
            }
        }
    }
}
